package org.lwxyz.api.shadowsocks;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.lwxyz.bean.shadowsocks.AuthorizeResult;
import org.lwxyz.service.shadowsocks.AuthorizeService;
import org.lwxyz.tools.mail.MailUtils;
import org.lwxyz.tools.mail.entity.Mail;
import org.lwxyz.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Random;

/**
 * @author yuwei
 * @version 1.0-SNAPSHOT
 * @description
 * @date
 */
@RestController
public class AccountController {

    private static Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AuthorizeService authorizeService;
    /**
     * 获取用户名、密码
     * @return
     */
    @RequestMapping("/authorize")
    public String authority(){
        AuthorizeResult authorizeResult = null;
        try {
            authorizeResult = authorizeService.authorize();
        } catch (Exception e) {
            System.out.println("文件读取错误！");
        }
        logger.info("返回结果：" + JsonUtils.obj2JsonString(authorizeResult));
        return JsonUtils.obj2JsonString(authorizeResult);
    }

    @RequestMapping("/authorityForEmail")
    public String authorityForEmail(@RequestParam(value = "email") String email) throws Exception {
        String result = this.authority();
        Map<String, Object> map = JsonUtils.json2Map(result);
        Object status = map.get("resultCode");
        if ("SUCCESS".equals(status)) {
            Object authorizeList = map.get("authorizeList");
            JSONArray jsonArray = JSONArray.parseArray(authorizeList.toString());
            // 随机获取其中一个帐号密码
            Random random = new Random();
            int ranNum = random.nextInt(jsonArray.size());
            Object json = jsonArray.get(ranNum);
            JSONObject jsonObject = JSONObject.parseObject(json.toString());
            // 准备邮件内容
            String title = "shadowsocks密码";
            StringBuffer context = new StringBuffer();
            context.append("服务器地址：" + jsonObject.get("serverIP") + "<br>");
            context.append("服务器端口：" + jsonObject.get("serverPort") + "<br>");
            context.append("密码：" + jsonObject.get("password") + "<br>");
            context.append("加密：" + jsonObject.get("encryption") + "<br>");
            context.append("备注：随便填点什么<br>");
            context.append("代理端口：" + jsonObject.get("proxyPort") + "<br>");
            Mail mail = new Mail();
            mail.setSubject(title);
            mail.setContext(context.toString());
            MailUtils.sendMail(1, email, mail);
            logger.info(email + "获取结果：" + JsonUtils.obj2JsonString(mail));
            return "账号密码已经通过邮件方式发送到您的邮箱:" + email;
        } else {
            return "发送失败，失败原因：获取帐号密码错误。";
        }
    }
}
